<template>
  <div class="wrap">
    <div class="header">
      <div class="left">
      </div>
      <div class="right">
        <div style="display: inline-block; width: 300px;">
          <el-input
            size="small"
            style="width: 200px; margin-right: 10px; margin-bottom: 10px"
            v-model="queryParams.keyword"
            :placeholder="placeholderStr"
            @keyup.enter.native="changeOption(true)"
            clearable @clear="changeOption(false, 'clear')"
          ></el-input>
          <el-button size="small" type="primary" @click="changeOption(true)">{{ $t('common.search') }}</el-button>
        </div>
      </div>
    </div>
    <span>
      失败原因 <span v-if="!loading && fileName !== '' && filePath !== ''" class="font-blue" style="cursor: pointer" @click="downloadFile(filePath)">({{ fileName }})</span>
    </span>
    <div class="content">
      <el-table
        v-loading="loading"
        :data="data"
        border
      >
        <template v-if="type === 'order'">
          <el-table-column
            prop="orderId"
            :label="'订单编号'"
            align="center"
            min-width="100"
          >
          </el-table-column>
        </template>

        <template v-if="type === 'goods'">
          <el-table-column
            prop="title"
            :label="'商品标题'"
            align="center"
            min-width="100"
          >
          </el-table-column>
        </template>

        <el-table-column
          prop="sku"
          :label="'SKU'"
          align="center"
          min-width="100"
        >
        </el-table-column>
        <el-table-column
          prop="errorMsg"
          :label="'失败原因'"
          align="center"
          min-width="100"
        >
        </el-table-column>
      </el-table>
      <div class="page-wrap mgt10">
        <el-pagination
          @current-change="getData"
          :current-page.sync="queryParams.page"
          :page-size="queryParams.pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
        >
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import {getBatchUploadPacketOrderErrorData} from "@/api/packetOrders";
import {getBatchUploadPacketGoodsErrorData} from "@/api/packetGoods";
export default {
  name: "cutOffList",
  data(){
    return {
      id: 0,
      type: '',
      loading: false,
      is_search: false,
      placeholderStr: '',
      queryParams:{
        page: 1,
        pageSize: 20,
        keyword: '',
      },
      data: [],
      total: 0,
      fileName: '',
      filePath: '',
    }
  },
  created() {
    this.id = this.$route.query.id;
    this.type = this.$route.query.type;
    if (this.type === 'goods') {
      this.placeholderStr = '搜索 商品标题/SKU'
    } else if (this.type === 'order') {
      this.placeholderStr = '搜索 订单编号/SKU'
    }
    this.getData()
  },
  methods: {
    getData(){
      if (this.type === 'goods') {
        this.loading = true
        getBatchUploadPacketGoodsErrorData(this.queryParams, this.id)
          .then(res=>{
            this.loading = false
            this.data = res.data
            this.total = res.info.total
            this.fileName = res.info.fileName
            this.filePath = res.info.filePath
          })
      } else if (this.type === 'order') {
        this.loading = true
        getBatchUploadPacketOrderErrorData(this.queryParams, this.id)
          .then(res=>{
            this.loading = false
            this.data = res.data
            this.total = res.info.total
            this.fileName = res.info.fileName
            this.filePath = res.info.filePath
          })
      }
    },
    handleSizeChange(val){
      this.queryParams.pageSize = val;
      this.queryParams.page = 1;
      this.getData();
    },
    changeOption(is_search = false, type = ''){
      if (is_search === true) {
        //判断是否使用过搜索按钮
        this.is_search = true
      }
      if (type === 'clear') {
        if (this.is_search === false) {
          return
        } else {
          this.is_search = false
        }
      }
      this.queryParams.page = 1;
      this.getData()
    },
    downloadFile(filePath) {
      if(!filePath){
        return
      }
      window.open(filePath,"_blank");
    },
  },
}
</script>

<style scoped lang="scss">
.wrap {
  padding: 30px 30px;
  .header{
    display: flex;
    justify-content: space-between;
  }
  .content{
    padding: 20px 0 0;
  }
}
</style>
